<?php
namespace App\Repositories;

use App\Exceptions\coopException;
use App\Model\User;
use App\Repositories\Interfaces\UserRepoInterface;
use Noodlehaus\Exception;
use Illuminate\Support\Facades\Redis;

class UserRepository  implements  UserRepoInterface
{
    public function register($data)
    {
        User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => \Hash::make($data['password']),
        ]);
        return true;
    }
    public function login($data)
    {
        $user = User::where('email',$data['email'])->first();

        if(!$user) {
            throw new CoopException("用户不存在");
        }

        if(!\Hash::check($data['password'],$user->password)){
            throw new CoopException("密码有误");
        }

        $token = sha1(mt_rand(0,9999).substr(time(),-4).substr(microtime(),2,6).mt_rand(1000,9999).time());
        Redis::setex('userInfo_'.$token,60,$user->id);
        return $token;
    }
    public function getUserInfo($id)
    {
        $user = User::find($id);
        return $user;
    }
}
